package com.application.sistercityapplication.main;

import java.util.List;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.application.sistercityapplication.dto.UserDto;
import com.application.sistercityapplication.json.JSONParserForGetList;
import com.application.sistercityapplication.model.User;
import com.application.sistercityapplication.utils.GlobalVariable;
import com.example.library_app_msg.AppMsg;
import com.roscopeco.ormdroid.Entity;

public class MainSettingPageViewActivity extends SherlockActivity {
	boolean isLogin = false;
	RelativeLayout loginLayout, linearForLoginPanel, configurationSetting;
	TextView loginText, confirmText, cancelText,forgetPassword;
	View viewSlide1, viewSlide2;
	Button loginBt, LogOutButt;
	EditText username, password, username_p, password_p, confirmPassword,
			email, f_name, l_name, phoneNum;
	ImageView arrow;
	public int loginTrick = 0;
	private GlobalVariable gbv;
	private ProgressBar progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		gbv = new GlobalVariable();
		if (gbv.checkScreenSize(getApplicationContext()) == 2) {
			setContentView(R.layout.activity_main_setting_page_view_large);
			
		} else {
			setContentView(R.layout.activity_main_setting_page_view);
		}

		System.gc();
		/*
		 * Declare and initial element
		 */
		username_p = (EditText) findViewById(R.id.usernameInput);
		progress = (ProgressBar) findViewById(R.id.progress);
		password_p = (EditText) findViewById(R.id.editText1);
		progress.setVisibility(View.GONE);
		confirmPassword = (EditText) findViewById(R.id.editText2);
		email = (EditText) findViewById(R.id.editText3);
		f_name = (EditText) findViewById(R.id.editText4);
		l_name = (EditText) findViewById(R.id.editText5);
		phoneNum = (EditText) findViewById(R.id.editText6);
		// confirmText = (TextView) findViewById(R.id.confirmText);
		// cancelText = (TextView) findViewById(R.id.cancelText);
		
		forgetPassword = (TextView) findViewById(R.id.textView1);
		
		LogOutButt = (Button) findViewById(R.id.LogOutButt);
		loginLayout = (RelativeLayout) findViewById(R.id.relativeLayout2);
		linearForLoginPanel = (RelativeLayout) findViewById(R.id.linearForLoginPanel);
		loginBt = (Button) findViewById(R.id.loginButton);
		configurationSetting = (RelativeLayout) findViewById(R.id.configurationSetting);
		loginText = (TextView) findViewById(R.id.textView2);
		arrow = (ImageView) findViewById(R.id.imageView1);
		viewSlide1 = findViewById(R.id.viewSlide1);
		viewSlide2 = findViewById(R.id.viewSlide2);
		username = (EditText) findViewById(R.id.username);
		password = (EditText) findViewById(R.id.password);
		linearForLoginPanel.setVisibility(View.GONE);
		viewSlide2.setVisibility(View.GONE);
		configurationSetting.setVisibility(View.GONE);
		
		forgetPassword.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(MainSettingPageViewActivity.this,ForgetPasswordMainActivity.class);
				startActivity(i);
				
			}
		});
		
		// Check login
		if (checkLogin()) {
			linearForLoginPanel.setVisibility(View.GONE);
			viewSlide2.setVisibility(View.GONE);
			loginLayout.setBackgroundColor(Color.WHITE);
			loginText.setTypeface(null, Typeface.NORMAL);
			loginText.setText(getResources().getString(R.string.settingUser));
			viewSlide1.setVisibility(View.VISIBLE);
			arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow367));
			loginTrick = 9;
			LogOutButt.setVisibility(View.VISIBLE);
			List<User> list = Entity.query(User.class).executeMulti();
			User user = list.get(0);
			username_p.setText(user.username);
			email.setText(user.email);
			f_name.setText(user.firstname);
			l_name.setText(user.lastname);
			phoneNum.setText(user.telephone);
		} else {
			loginLayout.setVisibility(View.VISIBLE);
			LogOutButt.setVisibility(View.GONE);
		}
		LogOutButt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				User user = new User();
				user.removeAll();
				loginTrick=0;
				configurationSetting.setVisibility(View.GONE);
				loginLayout.setBackgroundColor(Color.WHITE);
				loginText.setTypeface(null, Typeface.NORMAL);
				viewSlide2.setVisibility(View.GONE);
				viewSlide1.setVisibility(View.VISIBLE);
				arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow367));
				loginText.setText(getResources().getString(R.string.LoginText));
				LogOutButt.setVisibility(View.GONE);
				username.setText("");
				password.setText("");
				JSONParserForGetList json = JSONParserForGetList.getInstance();
				json.getLogout();
			}
		});

		loginLayout.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View arg0) {

				if (loginTrick == 0) {
					linearForLoginPanel.setVisibility(View.VISIBLE);
					loginLayout.setBackground(null);
					loginText.setTypeface(null, Typeface.BOLD);
					viewSlide1.setVisibility(View.GONE);
					viewSlide2.setVisibility(View.VISIBLE);
					arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow277));
					loginTrick = 1;

				} else if (loginTrick == 1) {
					linearForLoginPanel.setVisibility(View.GONE);
					viewSlide2.setVisibility(View.GONE);
					loginLayout.setBackgroundColor(Color.WHITE);
					loginText.setTypeface(null, Typeface.NORMAL);
					viewSlide1.setVisibility(View.VISIBLE);
					arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow367));
					loginTrick = 0;
				} else if (loginTrick == 9) {
					linearForLoginPanel.setVisibility(View.GONE);
					configurationSetting.setVisibility(View.VISIBLE);
					loginLayout.setBackground(null);
					loginText.setTypeface(null, Typeface.BOLD);
					viewSlide1.setVisibility(View.GONE);
					viewSlide2.setVisibility(View.VISIBLE);
					arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow277));
					loginTrick = 99;
				} else if (loginTrick == 99) {
					linearForLoginPanel.setVisibility(View.GONE);
					configurationSetting.setVisibility(View.GONE);
					viewSlide2.setVisibility(View.GONE);
					loginLayout.setBackgroundColor(Color.WHITE);
					loginText.setTypeface(null, Typeface.NORMAL);
					viewSlide1.setVisibility(View.VISIBLE);
					arrow.setImageDrawable(getResources().getDrawable(R.drawable.arrow367));
					loginTrick = 9;
				}
			}
		});
		loginBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				JSONParserForGetList json = JSONParserForGetList.getInstance();
				new LoginProcessTask(json,progress).execute();

			}

		});
	}

	public boolean checkLogin() {
		User user = new User();
		return user.isNotEmpty();
	}

	class LoginProcessTask extends AsyncTask<String, Void, UserDto> {
		ProgressDialog pDialog;
		private JSONParserForGetList json;
		private ProgressBar _resid;

		public LoginProcessTask(JSONParserForGetList _json, ProgressBar resid) {
			super();
			this.json = _json;
			this._resid = resid;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			_resid.setVisibility(View.VISIBLE);
		}

		@Override
		protected UserDto doInBackground(String... params) {
			UserDto userDto = json.getLogin(username.getText().toString(),
					password.getText().toString());

			return userDto;
		}

		@Override
		protected void onPostExecute(UserDto result) {
			super.onPostExecute(result);
			_resid.setVisibility(View.GONE);
			if (!result.isStatus()) {
				AppMsg.makeText(MainSettingPageViewActivity.this,
						result.getErrMsg(), AppMsg.STYLE_ALERT).show();

				// AlertDialog.Builder builder = new
				// AlertDialog.Builder(MainSettingPageViewActivity.this);
				// builder.setTitle("Error");
				// builder.setMessage(userDto.getErrMsg());
				// builder.setPositiveButton("OK", null);
				// AlertDialog dialog = builder.create();
				// dialog.show();
			} else {

				AppMsg.makeText(MainSettingPageViewActivity.this,
						getResources().getString(R.string.validLogin),
						AppMsg.STYLE_INFO).show();
				User user = new User();
				user.username = result.getUsername();
				user.firstname = result.getFirstname();
				user.lastname = result.getLastname();
				user.role_id = result.getRole_id();
				user.email = result.getEmail();
				user.telephone = result.getTelephone();
				user.key = result.getKey();
				user.updated_at = result.getUpdated_at();
				user.save();

				username_p.setText(result.getUsername());
				email.setText(result.getEmail());
				f_name.setText(result.getFirstname());
				l_name.setText(result.getLastname());
				phoneNum.setText(result.getTelephone());

				linearForLoginPanel.setVisibility(View.GONE);
				viewSlide2.setVisibility(View.GONE);
				loginLayout.setBackgroundColor(Color.WHITE);
				loginText.setTypeface(null, Typeface.NORMAL);
				loginText.setText(getResources()
						.getString(R.string.settingUser));
				viewSlide1.setVisibility(View.VISIBLE);
				arrow.setImageDrawable(getResources().getDrawable(
						R.drawable.arrow367));
				loginTrick = 9;
				LogOutButt.setVisibility(View.VISIBLE);
			}

		}

	}

}
